//
//  ViewController.m
//  JsonModelDemo
//
//  Created by LZXuan on 15-7-28.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

//导入对外层的model
#import "TripsModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //解决https证书问题
    //把服务端证书(需要转换成cer格式)放到APP项目资源里，AFSecurityPolicy会自动寻找根目录下所有cer文件
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    
    securityPolicy.allowInvalidCertificates = YES;
    //给manager
    manager.securityPolicy = securityPolicy;
    
    //设置 响应格式
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:@"https://chanyouji.com/api/trips/239069.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
#if 0
        //直接把json 格式的 NSData 转化为 model --》内嵌很多model
        //一句话搞定
        TripsModel *model = [[TripsModel alloc] initWithData:responseObject error:nil];
#else
        //先解析成一个字典
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //把一个字典 数据解析成model
        TripsModel *model = [[TripsModel alloc] initWithDictionary:dict error:nil];
#endif
        NSLog(@"model:%@",model);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
    }];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
